<?php
	class GenericS3andSDBModel extends S3andSDBModel
	{
		public function __construct()
		{
			parent::__construct();
			$this->setDomain($this->_config->domain->name->test);
			$this->setBucket($this->_config->bucket->name->test);
		}
	}

	class S3andSDBModelTest extends PHPUnit_Framework_TestCase
	{
		protected $_attributes;
		protected $_binary_content;
		protected $_binary_info;
		
		protected function setUp() 
		{ 
			$config = Zend_Registry::get('config');

			$this->_attributes = array(
				'unit' => 'test',
				'test' => 'unit'
			);
			
			$this->_binary_content = base64_decode(file_get_contents(
				$config->path->unit_tests->data->jpeg));

			$this->_binary_info = array(
				'access' => S3::ACL_PUBLIC_READ,
				'headers' => array('Content-Type' => 'image/jpeg'),
			);
		}
		
		protected function tearDown() { }

		public function testGetAndSetAttribute()
		{
			$gen_model = new GenericS3andSDBModel();
			
			foreach ($this->_attributes as $key => $value)
			{
				$this->assertEquals('', $gen_model->getAttribute($key));
				$gen_model->setAttribute($key, $value);
				$this->assertEquals($value, $gen_model->getAttribute($key));
			}

			$this->assertEquals(array_merge($this->_attributes, 
				array('bucket' => $gen_model->getBucket())), $gen_model->getAttributes());
			$this->assertFalse($gen_model->isSaved());
		}
		
		public function testSetAttributes()
		{
			$gen_model = new GenericS3andSDBModel();
			
			foreach ($this->_attributes as $key => $value)
				$this->assertEquals('', $gen_model->getAttribute($key));
			
			$gen_model->setAttributes($this->_attributes);

			foreach ($this->_attributes as $key => $value)
				$this->assertEquals($value, $gen_model->getAttribute($key));

			$this->assertFalse($gen_model->isSaved());
		}
		
		public function testGetId()
		{
			$gen_model = new GenericS3andSDBModel();
			$gen_model->setAttributes($this->_attributes);
			$gen_model->setBinaryInfo($this->_binary_content, $this->_binary_info);

			foreach ($this->_attributes as $key => $value)
				$this->assertEquals($value, $gen_model->getAttribute($key));
			$this->assertNull($gen_model->getId());
			$this->assertFalse($gen_model->isSaved());

			$gen_model->save();
			$this->assertNotNull($gen_model->getId());

			$gen_model->delete();
			$this->assertNull($gen_model->getId());
			$this->assertFalse($gen_model->isSaved());
		}

		public function testSaveLoadDelete() 
		{ 
			$gen_model = new GenericS3andSDBModel();
			$gen_model->setAttributes($this->_attributes);
			$gen_model->setBinaryInfo($this->_binary_content, $this->_binary_info);

			foreach ($this->_attributes as $key => $value)
				$this->assertEquals($value, $gen_model->getAttribute($key));
			$this->assertNull($gen_model->getId());
			$this->assertFalse($gen_model->isSaved());
			
			$gen_model->save();
			$id = $gen_model->getId();
			
			foreach ($this->_attributes as $key => $value)
				$this->assertEquals($value, $gen_model->getAttribute($key));
			$this->assertNotNull($gen_model->getId());
			$this->assertTrue($gen_model->isSaved());
			
			$gen_model->clearObject();

			foreach ($this->_attributes as $key => $value)
				$this->assertEquals('', $gen_model->getAttribute($key));
			$this->assertNull($gen_model->getId());
			$this->assertFalse($gen_model->isSaved());

			$gen_model->load($id);

			foreach ($this->_attributes as $key => $value)
				$this->assertEquals($value, $gen_model->getAttribute($key));
			$this->assertNotNull($gen_model->getId());
			$this->assertTrue($gen_model->isSaved());

			$gen_model->delete();

			foreach ($this->_attributes as $key => $value)
				$this->assertEquals('', $gen_model->getAttribute($key));
			$this->assertNull($gen_model->getId());
			$this->assertFalse($gen_model->isSaved());
		}
	}
